package com.ty.controller;

import cn.hutool.core.util.StrUtil;
import com.netflix.loadbalancer.AvailabilityFilteringRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api")
public class BController {

   @Autowired
   private RestTemplate restTemplate;

   @Autowired
   private LoadBalancerClient loadBalancerClient;

    @GetMapping("/b")
    public String test(){
     String ret =restTemplate.getForObject("http://nacos-a/api/a", String.class);
     return "b"+ret;
    }

    @GetMapping("/b2")
    public String test2(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-a");

        String url = StrUtil.format("http://{}:{}/api/a", serviceInstance.getHost(), serviceInstance.getPort());

        System.out.println("request url:"+ url);
        String ret =restTemplate.getForObject(url, String.class);

        return "b"+ret;

    }
}
